package com.sub.utils;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 自定义元数据对象处理器,并交由Spring管理：统一处理公共字段自动填充
 *
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private HttpSession session;

    /**
     * 对公共字段统一添加处理
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime",new Date());
        metaObject.setValue("updateTime",new Date());
        Long id = BaseContext.getId();

        metaObject.setValue("createUser",id);
        metaObject.setValue("updateUser",id);

      /*  Long empId = (Long) session.getAttribute("employee");
        if (empId==null){
            Long userId = (Long) session.getAttribute("user");
            metaObject.setValue("createUser",userId);
            metaObject.setValue("updateUser",userId);
        }else{
            metaObject.setValue("createUser",empId);
            metaObject.setValue("updateUser",empId);
        }*/

    }

    /**
     * 对公共字段统一修改处理
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime",new Date());
        Long id = BaseContext.getId();
        metaObject.setValue("updateUser",id);
       /* Long empId = (Long) session.getAttribute("employee");
        if (empId==null){
            Long userId = (Long) session.getAttribute("user");
            metaObject.setValue("updateUser",userId);
        }else {
            metaObject.setValue("updateUser",empId);
        }*/
    }
}
